<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // Object.entries()方法返回一个给定对象自身可枚举属性的键值对数组  区别在于 for-in 循环还会枚举原型链中的属性）。
      let obj = {
        a: '你好',
        b: '你好呀',
      };
      console.log(obj);
      for (const [key, value] of Object.entries(obj)) {
        console.log(`${key}: ${value}`); // //a: 你好   b: 你好呀
      }
      // 处理数组
      let arr = [1, 2, 3];
      for (const [key, value] of Object.entries(arr)) {
        console.log(`${key}: ${value}`); // 0: 1    1: 2    2: 3
      }
      //处理字符串
      let str = Object.entries('abc');
      console.log(str); // ['0', 'a'][('1', 'b')][('2', 'c')];
      //处理数字
      const num = 123;
      console.log(Object.entries(num)); // []   返回空数组
      const float1 = 12.3;
      console.log(Object.entries(float1)); // []  返回空数组

      //验证不会寻找原型上面的值
      obj.__proto__.c = '你好你好';
      for (const [key, value] of Object.entries(obj)) {
        console.log(`${key}: ${value}`); // a: 你好  b: 你好呀    上面原型上增加的c不会显示出来
      }

      // map
      const m = new Map();
      m.set('a', 1);
      m.set('b', 2);
      console.log(m); //Map(2) {'a' => 1, 'b' => 2}
      const mm = Object.entries(obj);
      console.log(mm); //[['a', '你好'], ['b', '你好呀']]
      const m2 = new Map(Object.entries(mm));
      console.log(m2); //Map(2) {'a' => '你好', 'b' => '你好呀'}
    </script>
  </body>
</html>
